package redislock;

import org.redisson.Redisson;
import org.redisson.api.RLock;

public class RedisLock
{
    //从配置类中获取redission对象
    private static Redisson redisson = RedisManager.getRedisson();
    private static final String LOCK_TITLE = "redisLock";

    public static boolean lock(String lockName)
    {
        //key的名字
        String key = LOCK_TITLE + lockName;
        //获取锁对象
        RLock myLock = redisson.getLock(key);
        //加锁，设置锁过期时间
        myLock.lock();
        System.err.println("加锁成功++++++++++=====" + Thread.currentThread().getName());
        return true;
    }

    //锁的释放
    public static void unlock(String lockName)
    {
        //必须是和加锁时的同一个key
        String key = LOCK_TITLE + lockName;
        //获取锁对象
        RLock myLock = redisson.getLock(key);


        //释放锁
        myLock.unlock();
        System.err.println("释放锁================================" + Thread.currentThread().getName());

    }
}
